package ai.people.platform.graph.service.impl;

import ai.people.core.grpc.lib.graph.*;
import ai.people.core.grpc.utils.ProtoJsonUtil;
import ai.people.core.grpc.utils.StreamObserverDelegate;
import ai.people.platform.graph.service.GraphDataService;
import com.google.protobuf.BoolValue;
import com.google.protobuf.StringValue;
import io.grpc.stub.StreamObserver;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;

/**
 * @author zhenglin
 * @date 2022/6/10
 */
@GrpcService
@Slf4j
@RequiredArgsConstructor
public class GraphRpcService extends GraphServiceGrpc.GraphServiceImplBase {

    private final GraphDataService graphDataService;

    /**
     * <pre>
     * *
     * 插入数据
     * </pre>
     *
     * @param request          请求
     * @param responseObserver 响应观察者
     */
    @Override
    public void insertGraphData(InsertRequest request, StreamObserver<StringValue> responseObserver) {
        StreamObserverDelegate.build(responseObserver).execute(() -> {
            ai.people.netmon.framework.domain.graph.request.InsertRequest insertRequest = ProtoJsonUtil.toJava(request, ai.people.netmon.framework.domain.graph.request.InsertRequest.class);
            try {
                String insert = graphDataService.insert(insertRequest);
                return StringValue.newBuilder().setValue(insert).build();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            return null;
        });
    }

    /**
     * <pre>
     * *
     * 插入数据(带有关系)
     * </pre>
     *
     * @param request          请求
     * @param responseObserver 响应观察者
     */
    @Override
    public void insertGraphDataWithRelation(InsertRelationRequest request, StreamObserver<BoolValue> responseObserver) {
        StreamObserverDelegate.build(responseObserver).execute(() -> {
            ai.people.netmon.framework.domain.graph.request.InsertRelationRequest insertRelationRequest = ProtoJsonUtil.toJava(request, ai.people.netmon.framework.domain.graph.request.InsertRelationRequest.class);
            try {
                graphDataService.insertWithEdge(insertRelationRequest);
                return BoolValue.newBuilder().setValue(true).build();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            return BoolValue.newBuilder().setValue(false).build();
        });
    }

    /**
     * <pre>
     * *
     * 批量插入数据
     * </pre>
     *
     * @param request
     * @param responseObserver
     */
    @Override
    public void batchSaveGraphData(BatchSaveRequest request, StreamObserver<BoolValue> responseObserver) {
        StreamObserverDelegate.build(responseObserver).execute(() -> {
            ai.people.netmon.framework.domain.graph.request.InsertRequestBatch insertRequest = ProtoJsonUtil.toJava(request, ai.people.netmon.framework.domain.graph.request.InsertRequestBatch.class);
            try {
                boolean save = graphDataService.batchSave(insertRequest);
                return BoolValue.newBuilder().setValue(save).build();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            return null;
        });
    }

    /**
     * <pre>
     * *
     * 批量插入数据(带有关系)
     * </pre>
     *
     * @param request
     * @param responseObserver
     */
    @Override
    public void batchSaveGraphDataWithRelation(BatchSaveRelationRequest request, StreamObserver<BoolValue> responseObserver) {
        StreamObserverDelegate.build(responseObserver).execute(() -> {
            ai.people.netmon.framework.domain.graph.request.InsertRelationBatch insertRequest = ProtoJsonUtil.toJava(request, ai.people.netmon.framework.domain.graph.request.InsertRelationBatch.class);
            try {
                boolean save = graphDataService.batchSaveWithEdge(insertRequest);
                return BoolValue.newBuilder().setValue(save).build();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            return null;
        });
    }
}
